package com.omkarpatil.learningjpa2.mapping.manytoone.bidi;

import javax.persistence.EntityManager;

import com.omkarpatil.learningjpa2.util.EntityManagerFactoryFactory;

public class TestDriver {

	public static void main(String[] args) {
		EntityManager em1 = EntityManagerFactoryFactory.getEntityManagerFactory().createEntityManager();
		em1.getTransaction().begin();
		
		//Saving a project
		Project project = new Project("ProjectX");
		em1.persist(project);
		System.out.printf("##### Created one project with id = %s\n", project.getId());
		
		//Adding an employee
		Employee emp1 = new Employee("Jackie Chan");
		//assigning the employee to project
		emp1.setProject(project);
		em1.persist(emp1);
		System.out.printf("##### Assigned one Employe with id = %s\n", emp1.getId());
		
		//creating another employee
		Employee emp2 = new Employee("Tom Hanks");
		emp2.setProject(project);
		em1.persist(emp2);
		System.out.printf("##### Assigned another Employe with id = %s\n", emp2.getId());
		
		em1.getTransaction().commit();
		em1.close();
		
		EntityManager em2 = EntityManagerFactoryFactory.getEntityManagerFactory().createEntityManager();
		em2.getTransaction().begin();
		
		Project project2 = em2.find(Project.class, project.getId());
		
		System.out.printf("##### How many emplyees allocated to ProjectX ? %s\n", project2.getEmployees().size());
		em2.getTransaction().commit();
		em2.close();
	}
}
